home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue67 / express / demo / demou.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-08-01  |  4.0 KB  |  159 lines

  1. unit Demou;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, Parser10;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     IterationEdit: TEdit;
  13.     Button1: TButton;
  14.     Button2: TButton;
  15.     CountLabel: TLabel;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Parser1: TParser;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.     procedure IterationEditKeyPress(Sender: TObject; var Key: Char);
  22.     procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  23.     procedure FormCreate(Sender: TObject);
  24.   private
  25.     DynaParser: TParser;
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.DFM}
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. begin
  37.   { use an instance of TParser on the form if it exists }
  38.   DynaParser := FindComponent('Parser1') as TParser;
  39.  
  40.   if DynaParser = nil then
  41.     DynaParser := TParser.Create(Self);
  42. end;
  43.  
  44. procedure TForm1.IterationEditKeyPress(Sender: TObject; var Key: Char);
  45. begin
  46.   if not (Key in ['0'..'9',#8]) then
  47.     Key := #0;
  48. end;
  49.  
  50. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  51. begin
  52.   if Key = #32 then
  53.   begin
  54.     Key := #0;
  55.     ShowMessage('The parser cannot handle expressions containing blanks.');
  56.   end;
  57. end;
  58.  
  59. {$IFDEF Win32}
  60.   {$HINTS OFF}
  61. {$ENDIF}
  62. procedure TForm1.Button1Click(Sender: TObject);
  63. var
  64.   StartTime,
  65.   counter: longint;
  66.  
  67.   dummy : extended;
  68. begin
  69.   DynaParser.Expression := Edit1.Text;
  70.  
  71.   StartTime := GetTickCount;
  72.  
  73.   { this does the calculation }
  74.   for counter := 1 to StrToInt(IterationEdit.Text) do
  75.   begin
  76.     dummy := DynaParser.Value; { ignore the compiler hint }
  77.   end;
  78.  
  79.   StartTime := GetTickCount - StartTime;
  80.  
  81.   ShowMessage(Format('Result: %g', [DynaParser.Value]));
  82.  
  83.   ShowMessage( Format('Did that seem slow?'#13+
  84.                'Well, this expression was evaluated '+IterationEdit.Text+' times in just %d milliseconds!'#13+
  85.                '  (see the little edit box below the button)', [StartTime]));
  86. end;
  87. {$IFDEF Win32}
  88.   {$HINTS ON}
  89. {$ENDIF}
  90.  
  91. procedure TForm1.Button2Click(Sender: TObject);
  92. var
  93.   StartTime1,
  94.   StartTime2,
  95.   i : longint;
  96.  
  97.   pf: PParserFloat;
  98. begin
  99.   { This is the slowest way...}
  100.   ShowMessage('This is the slowest way to set variables...');
  101.  
  102.   Screen.Cursor := crHourGlass;
  103.   try
  104.     DynaParser.Variable['Counter1'] := StrToFloat(CountLabel.Caption);
  105.     DynaParser.Expression := 'Counter1+1';
  106.  
  107.     StartTime1 := GetTickCount;
  108.     for i := 1 to 10000 do
  109.     begin
  110.       DynaParser.Variable['Counter1'] := DynaParser.Value; { calculate and assign to "Counter" }
  111.  
  112.   (*  CountLabel.Caption := FloatToStr( DynaParser.Variable['Counter1'] );
  113.       Application.ProcessMessages;    *)
  114.     end;
  115.     CountLabel.Caption := FloatToStr( DynaParser.Variable['Counter1'] );
  116.     StartTime1 := GetTickCount - StartTime1;
  117.   finally
  118.     Screen.Cursor := crDefault;
  119.   end;
  120.  
  121.   ShowMessage(Format('Time: %d ms', [StartTime1]));
  122.  
  123.  
  124.  
  125.  
  126.  
  127.   { This is the FAST way...}
  128.   ShowMessage('This is the FAST way to do it...');
  129.   Screen.Cursor := crHourGlass;
  130.   try
  131.     { we remember where the component actually stored the variable and
  132.       access the memory directly }
  133.     pf := DynaParser.SetVariable('Counter2', StrToFloat(CountLabel.Caption));
  134.     { BTW, this is yet another way to change variables;
  135.       if the variable already exists only the pointer to existing memory
  136.       is returned }
  137.  
  138.     DynaParser.Expression := 'Counter2+1';
  139.  
  140.     StartTime2 := GetTickCount;
  141.     for i := 1 to 10000 do
  142.     begin
  143.       pf^ := DynaParser.Value; { calculate and assign to "Counter" }
  144.  
  145.   (*  CountLabel.Caption := FloatToStr( DynaParser.Variable['Counter2'] );
  146.       Application.ProcessMessages; *)
  147.     end;
  148.  
  149.     CountLabel.Caption := FloatToStr( DynaParser.Variable['Counter2'] );
  150.     StartTime2 := GetTickCount - StartTime2;
  151.   finally
  152.     Screen.Cursor := crDefault;
  153.   end;
  154.  
  155.   ShowMessage(Format('Time: %d ms'#13#13'Just %d times faster...', [StartTime2, Trunc(StartTime1/StartTime2)]));
  156. end;
  157.  
  158. end.
  159.